<?php

//
// Widget :: Header Text
//
if ( function_exists('register_sidebar') )
  register_sidebar(array(
    'name' => 'Header Text',
    'before_widget' => '<p>',
    'after_widget' => '</p>',
    'before_title' => '<h1>',
    'after_title' => '</h1>',
  ));


//
// Widget :: Announcements
//
if ( function_exists('register_sidebar') )
  register_sidebar(array(
    'name' => 'Announcements',
    'before_widget' => '<p>',
    'after_widget' => '</p>',
    'before_title' => '<b>',
    'after_title' => '</b>',
  ));


/**************************************************************
* Determine if member directory is being requestede
**************************************************************/
function FWC__isMemberDirectory($pFlag = false)
{
  return $pFlag;
}


/**************************************************************
* Sort everybody's profile by last name
**************************************************************/
function FWC__2dSort($a, $b)
{
echo '?';
  return $a['User']['last_name'] > $b['User']['last_name'];
}



/**************************************************************
* Sort everybody's profile by last name
**************************************************************/
function FWC__sortProfiles(&$pUsers)
{
  $profiles = array();

  // Get everyone in their own object
  foreach($pUsers as $user)
  {

    // Skip this iteration if we're not dealing with a subscriber
    if($user->meta_key == 'wp_capabilities' && stripos($user->meta_value, 'subscriber') <= 0)
      $profiles[$user->id]['admin'] = true;

    $profiles[$user->id]['id'] = $user->id;
    $profiles[$user->id]['email'] = $user->user_email;

    if($user->meta_key == 'first_name')
      $profiles[$user->id]['first_name'] = $user->meta_value;

    if($user->meta_key == 'last_name')
      $profiles[$user->id]['last_name'] = $user->meta_value;

    if($user->meta_key == 'aim')
      $profiles[$user->id]['address'] = $user->meta_value;

    if($user->meta_key == 'yim')
      $profiles[$user->id]['phone'] = $user->meta_value;

  }

  // Make new array that can be keyed in a way that's easy to sort
  $sorted = array();
  foreach($profiles as $profile)
    if(strlen($profile['last_name']) > 0)
      $sorted[$profile['last_name'].$profile['first_name'].$profile['id']] = $profile;

  // Sort by key
  ksort($sorted);
  $pUsers = $sorted;
}



/**************************************************************
* Query the database for all users and their associated metadata
**************************************************************/
function FWC__queryMemberDirectory()
{
  global $wpdb;
  return $wpdb->get_results("

    SELECT 
      User.id,
      User.user_email,
      Meta.meta_key,
      Meta.meta_value

    FROM 
      wp_users User
    LEFT JOIN wp_usermeta Meta ON Meta.user_id = User.ID

    WHERE
      Meta.meta_key = 'first_name' OR
      Meta.meta_key = 'last_name' OR
      Meta.meta_key = 'aim' OR
      Meta.meta_key = 'yim' OR
      Meta.meta_key = 'wp_capabilities'

    ORDER BY
      User.id
  ");
}

/**************************************************************
* Print a nicely formatted member directory
**************************************************************/
function FWC__printMemberDirectory()
{
  // Only show member directory if user is logged in
  if(!is_user_logged_in())
    return;

  $userProfiles = FWC__queryMemberDirectory();
  FWC__sortProfiles($userProfiles);


  // Print the table
  echo '<table style="width:100%;border-collapse: collapse;border: 2px solid #333;">
	<thead>
	  <tr style="background: #aaf;">
	    <td style="border: 1px solid #333;"><b>Name</b></td>
	    <td style="border: 1px solid #333;"><b>Address</b></td>
	    <td style="border: 1px solid #333;"><b>Phone</b></td>
	    <td style="border: 1px solid #333;"><b>Email</b></td>
	  </tr>
	</thead>

	<tbody>';

  foreach($userProfiles as $user)
	// Only print information for subscribers
	if($user['email'] != 'poo04004@byui.edu')
	echo '<tr>
	    <td style="border: 1px solid #333;">',
	    $user['last_name'], ', ', $user['first_name'],
	    '</td>

	    <td style="border: 1px solid #333;">',
	    $user['address'],
	    '</td>

	    <td style="border: 1px solid #333;">',
	    $user['phone'],
	    '</td>

	    <td style="border: 1px solid #333;"><a href="mailto:',
	    $user['email'],
	    '">',
	    $user['email'],
	    '</a></td>
	</tr>';

  echo '</tbody></table>';

  exit;
}